﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using SpotOffer.Resource;
using SpotOffer.View.CustomView;
using Xamarin.Forms;
using XLabs.Enums;
using XLabs.Forms.Controls;
using XLabs.Forms.Mvvm;
using CircleImage = ImageCircle.Forms.Plugin.Abstractions.CircleImage;

namespace SpotOffer.View
{
    public class DealListPage : BaseView
    {
        public MenuTableView DealListView;
        private const double Rowheight = 150;

        private const double ItemMargin = 5;

        private ExtendedLabel _pricelabel, _saveLabel, _oldpricelabel, _itemBackgroundLabel, _itemBackgroundLabelborder, _labelDealDescription;
        private CircleImage _dealImage, _loadingImage;

        static bool IsPortrait(Page p) { return p.Width < p.Height; }

        public DealListPage()
        {
            MakeContent();
        }

        //Handles Rotation
        private void HandleRotation(object sender, EventArgs e)
        {
            if (IsPortrait(this))
            {

            }
            else
            {

            }
        }


        private void MakeContent()
        {

            var searchInput = new CustomEntry();
            searchInput.SetBinding(Entry.TextProperty, "Search");
            searchInput.TextColor = AppColors.FacebookBlue;
            searchInput.BackgroundColor = AppColors.White;        
            searchInput.Placeholder = "Søg ex. Sushi, Hotel, Massage, Rejse...";
        
            /*  searchInput.PlaceholderTextColor = AppColors.LightGrey;
              searchInput.Font = Font.SystemFontOfSize(NamedSize.Medium);
              searchInput.BackgroundColor = AppColors.Turkish;*/

            DealListView = new MenuTableView
            {
                BackgroundColor = AppColors.LighterGrey,
                ItemTemplate = new DataTemplate(() =>
                {
                    MakeLabels();
                    var itemLayout = new RelativeLayout
                    {
                        HorizontalOptions = LayoutOptions.Fill,
                        VerticalOptions = LayoutOptions.Fill
                    };
                    itemLayout.Children.Add(_itemBackgroundLabel, Constraint.Constant(ItemMargin + Rowheight / 2), Constraint.Constant(ItemMargin), Constraint.RelativeToParent((parent) => parent.Bounds.Width - Rowheight / 2 - ItemMargin * 2), Constraint.Constant(Rowheight - ItemMargin + 5));
                    itemLayout.Children.Add(_itemBackgroundLabelborder, Constraint.Constant((ItemMargin + Rowheight / 2) + 1), Constraint.Constant(ItemMargin + 1), Constraint.RelativeToParent((parent) => (parent.Bounds.Width - Rowheight / 2 - ItemMargin * 2) - 2), Constraint.Constant(Rowheight - ItemMargin + 3));

                    /*  itemLayout.Children.Add(_dealImage, Constraint.Constant(ItemMargin), Constraint.Constant(ItemMargin), Constraint.Constant(Rowheight), Constraint.Constant(Rowheight));
                        itemLayout.Children.Add(_pricelabel, Constraint.Constant(ItemMargin + Rowheight + 2), Constraint.Constant(ItemMargin + Rowheight / 2), null, Constraint.Constant((Rowheight - 5) / 2));
                        itemLayout.Children.Add(_labelDealDescription, Constraint.Constant(ItemMargin + Rowheight + 2), Constraint.Constant(ItemMargin +1), Constraint.RelativeToParent((parent) => parent.Bounds.Width - (ItemMargin * 2 + Rowheight + 4)), Constraint.Constant(Rowheight / 2));
                        itemLayout.Children.Add(_saveLabel, Constraint.Constant(ItemMargin + Rowheight), Constraint.Constant(ItemMargin + Rowheight / 2), Constraint.RelativeToParent((parent) => parent.Bounds.Width - Rowheight - ItemMargin * 2), Constraint.Constant((Rowheight - 5) / 2));
                       */
                    itemLayout.Children.Add(_saveLabel,
                        Constraint.Constant(ItemMargin + Rowheight + 2),
                        Constraint.Constant(ItemMargin),
                        Constraint.RelativeToParent((parent) => parent.Bounds.Width - (ItemMargin * 2 + Rowheight + 4)),
                        Constraint.Constant((Rowheight / 2) - 1));

                    itemLayout.Children.Add(_dealImage, Constraint.Constant(ItemMargin), Constraint.Constant(ItemMargin - 1), Constraint.Constant(Rowheight + 2), Constraint.Constant(Rowheight + 2));


                    itemLayout.Children.Add(_pricelabel,
                        Constraint.Constant(ItemMargin * 2 + Rowheight),
                        Constraint.Constant(ItemMargin));


                    itemLayout.Children.Add(_oldpricelabel,
                        Constraint.RelativeToView(_pricelabel, (parent, sibiling) => sibiling.Bounds.Right + ItemMargin*2),
                        Constraint.Constant(ItemMargin),
                        null,
                            Constraint.RelativeToView(_pricelabel, (parent, sibiling) => sibiling.Bounds.Height));

                    itemLayout.Children.Add(_labelDealDescription, Constraint.Constant(ItemMargin * 2 + Rowheight), Constraint.Constant((Rowheight / 2)), Constraint.RelativeToParent((parent) => parent.Bounds.Width - (ItemMargin * 6 + Rowheight)), Constraint.Constant((Rowheight / 2) + 2));

                    return new ViewCell
                    {
                        View = itemLayout
                    };
                }),

            };

            DealListView.SetBinding(ListView.ItemsSourceProperty, new Binding("DealListFiltered"));
            DealListView.SetBinding(ListView.SelectedItemProperty, new Binding("SelectedDeal", BindingMode.TwoWay));
            DealListView.ItemSelected += (sender, e) =>
            {
                if (e.SelectedItem == null)
                    return; // don't do anything if we just de-selected the row
                // do something with e.SelectedItem
                ((ListView)sender).SelectedItem = null; // de-select the row
            };
            DealListView.RowHeight = (int)(Rowheight + ItemMargin * 2);
            DealListView.HasUnevenRows = false;

            var separator = new Separator()
            {
                Color = AppColors.FacebookBlue,
                BackgroundColor = Color.Transparent,
                StrokeType = StrokeType.Dotted

            };

            var deleteButton = new Label
            {
              //  BackgroundColor = AppColors.Turkish,
                Text = AppResources.Clear,
                FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),          
                TextColor = AppColors.White,
                BackgroundColor = AppColors.FacebookBlue,      
                YAlign = TextAlignment.Center,
                XAlign = TextAlignment.Center
            };
           deleteButton.SetBinding(IsVisibleProperty, "DeleteButtonVisbile");
           var tgr = new TapGestureRecognizer();
           tgr.Tapped += (s, e) =>searchInput.Text="";
           deleteButton.GestureRecognizers.Add(tgr);

            var content =  new RelativeLayout();
           
            content.Children.Add(searchInput, Constraint.Constant(0), Constraint.Constant(0), Constraint.RelativeToParent((parent) => parent.Width)/*, Constraint.Constant(35)*/);                  
            content.Children.Add(separator, Constraint.Constant(0), Constraint.RelativeToView(searchInput, (parent, sibling) => sibling.Height), Constraint.RelativeToParent((parent) => parent.Bounds.Width));        
            content.Children.Add(DealListView, Constraint.Constant(2), Constraint.RelativeToView(separator, (parent, sibling) => sibling.Bounds.Bottom), Constraint.RelativeToParent((parent) => parent.Bounds.Width), Constraint.RelativeToView(searchInput, (parent, sibling) => parent.Bounds.Height - sibling.Bounds.Height));
            content.Children.Add(deleteButton, Constraint.RelativeToView(searchInput, (parent, sibling) => sibling.Bounds.Right - deleteButton.Width - ItemMargin), Constraint.Constant(2), Constraint.Constant(50), Constraint.RelativeToView(searchInput,(parent,sibling)=>sibling.Height-4));
            
            content.BackgroundColor = AppColors.LighterGrey;
            Content = content;
        }

        private void MakeLabels()
        {
            // Create views with bindings for displaying each property.     
            _itemBackgroundLabel = new ExtendedLabel
            {
                BackgroundColor = AppColors.FacebookBlue,
            };
            _itemBackgroundLabelborder = new ExtendedLabel
            {
                BackgroundColor = AppColors.White,
            };

            _oldpricelabel = new ExtendedLabel
            {
                TextColor = AppColors.LetterA,
                Font = Font.SystemFontOfSize(NamedSize.Micro),
                FontAttributes = FontAttributes.Bold,
                YAlign = TextAlignment.Center,
                XAlign = TextAlignment.Start,
                IsStrikeThrough = true,
                //BackgroundColor = Color.Yellow

            };
            _oldpricelabel.SetBinding(Label.TextProperty, "price_orig");

            _pricelabel = new ExtendedLabel
            {
                TextColor = AppColors.Abacate,
                Font = Font.SystemFontOfSize(NamedSize.Medium),
                FontAttributes = FontAttributes.Bold,
                YAlign = TextAlignment.Center,

                //   BackgroundColor = Color.Yellow

            };
            _pricelabel.SetBinding(Label.TextProperty, "price");

            _labelDealDescription = new ExtendedLabel
             {
                 FontSize = Device.GetNamedSize(NamedSize.Micro, typeof(Label)),
     
               //  FontAttributes = FontAttributes.Bold,
                 YAlign = TextAlignment.End,
                 XAlign = TextAlignment.Start,
                 TextColor = AppColors.Black,
                 LineBreakMode = LineBreakMode.CharacterWrap,
                 IsDropShadow = false
                 /*IsStrikeThrough = true,
                 IsUnderline = true*/
                 //    BackgroundColor = Color.Pink

             };
            _labelDealDescription.SetBinding(Label.TextProperty, "category");

            _saveLabel = new ExtendedLabel
             {
                 TextColor = AppColors.FacebookBlue,
                 FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
                 FontAttributes = FontAttributes.Bold,
                 XAlign = TextAlignment.Center,
                 YAlign = TextAlignment.End,
                 IsUnderline = true,               
             };
            _saveLabel.SetBinding(Label.TextProperty, "discountdescr");

            _dealImage = new CircleImage
            {
                BorderColor = AppColors.FacebookBlue,
                BorderThickness = 2,
                Aspect = Aspect.AspectFill,
               // BackgroundColor = AppColors.FacebookBlue
            };
            _dealImage.SetBinding(Image.SourceProperty, new Binding("image"));
        }


        protected override void OnSizeAllocated(double width, double height)
        {
            base.OnSizeAllocated(width, height);
            if (width > 0)
            {
                Debug.WriteLine("Width " + width + " Height " + height);

            }
        }


    }
}
